package cc.telecomdigital.tdstock.Http;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import cc.telecomdigital.tdstock.ProgressDialog;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.view.SunFlowerView;

public class ProgressView
{
	public ProgressView(Context iContext)
	{
		actContext = iContext;
	}

	static final private String	LOG_TAG			= "ProgressView";
	static protected Handler	_messageHandler	= new Handler();
	private WindowManager		mWindowManager	= null;
	private SunFlowerView		sunFlowerView	= null;
	private ProgressDialog		progDialog		= null;
	private boolean				isDialog		= false;
	private Context				actContext;

	protected Context GetShowDialogContext()
	{
		return actContext;
	}

	protected WindowManager.LayoutParams getParams()
	{
		final WindowManager.LayoutParams wl = new WindowManager.LayoutParams();

		wl.x = 0;
		wl.y = 0;

		wl.height = WindowManager.LayoutParams.WRAP_CONTENT;
		wl.width = WindowManager.LayoutParams.WRAP_CONTENT;
		wl.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		wl.format = PixelFormat.TRANSLUCENT;
		wl.windowAnimations = 0;
		return wl;
	}

	private String INull(Object o)
	{
		return o == null ? "_Null" : "_NotNull";
	}

	protected void ShowProgressSunFlower()
	{

		final Runnable startJob = new Runnable()
		{
			public void run()
			{
				try
				{
					TDStockLog.d(LOG_TAG,
							"ShowSunFlower:" + actContext.getClass().getSimpleName() + "," + INull(mWindowManager) + "," + INull(sunFlowerView) + "," + (sunFlowerView != null
									? sunFlowerView.isRunning() : "false"));

					// init SunFlower Animation.
					if (mWindowManager == null)
						mWindowManager = (WindowManager) GetShowDialogContext().getSystemService(Context.WINDOW_SERVICE);

					try
					{
						if (sunFlowerView != null && (sunFlowerView.isRunning()))
						{
							sunFlowerView.setVisibility(View.GONE);
							sunFlowerView.stopAnimation();
							mWindowManager.removeView(sunFlowerView);
							sunFlowerView = null;
						}

					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
					sunFlowerView = new SunFlowerView(GetShowDialogContext());
					sunFlowerView.startAnimation();
					sunFlowerView.setVisibility(View.VISIBLE);

					mWindowManager.addView(sunFlowerView, getParams());
				}
				catch (Exception e)
				{
					TDStockLog.e(LOG_TAG, "showSunflower>error:" + e.toString());
					e.printStackTrace();
				}
			}
		};
		new Thread()
		{
			public void run()
			{
				_messageHandler.post(startJob);
			}
		}.start();
	}

	protected void DismissProgressSunFlower()
	{
		final Runnable stopJob = new Runnable()
		{
			public void run()
			{
				try
				{
					TDStockLog.d(LOG_TAG,
							"DissmissSunFlower:" + actContext.getClass().getSimpleName() + "," + INull(mWindowManager) + "," + INull(sunFlowerView) + "," + (sunFlowerView != null
									? sunFlowerView.isRunning() : "false"));

					if (mWindowManager == null)
						mWindowManager = (WindowManager) GetShowDialogContext().getSystemService(Context.WINDOW_SERVICE);

					if (mWindowManager != null && sunFlowerView != null && (sunFlowerView.isRunning()))
					{
						sunFlowerView.setVisibility(View.GONE);
						sunFlowerView.stopAnimation();
						mWindowManager.removeView(sunFlowerView);
						sunFlowerView = null;
					}
				}
				catch (Exception e)
				{
					TDStockLog.e(LOG_TAG, "dismissSunflower>error:" + e.toString());
					e.printStackTrace();
				}
				finally
				{
					sunFlowerView = null;
				}
			}
		};
		new Thread()
		{
			public void run()
			{
				_messageHandler.post(stopJob);
			}
		}.start();
	}

	protected void ShowProgressDialog()
	{
		if (progDialog == null)
			progDialog = new ProgressDialog(GetShowDialogContext(), GetShowDialogContext().getString(R.string.InProgressText), true);
		progDialog.setCancelable(true);
		progDialog.show();
	}

	protected void DismissProgressDialog()
	{
		if (progDialog != null)
			progDialog.dismiss();
	}

	public void SetShowDialog(boolean iDialog)
	{
		isDialog = iDialog;
	}

	public void ShowProgress()
	{
		if (isDialog)
			ShowProgressDialog();
		else ShowProgressSunFlower();
	}

	public void DismissProgress()
	{
		if (isDialog)
			DismissProgressDialog();
		else DismissProgressSunFlower();
	}

	public void DismissProgressAll()
	{
		DismissProgressDialog();
		DismissProgressSunFlower();
	}

	public void DismissProgressAllOnDestory()
	{
		DismissProgressDialog();
		DismissProgressSunFlower();
		progDialog = null;
		mWindowManager = null;
	}
}
